package com.example.kao.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.kao.entity.Hospital;
import com.example.kao.entity.MedicalAssay;
import com.example.kao.mapper.MedicalAssayMapper;
import com.example.kao.service.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class MedicaController {

    @Autowired
    MedicalAssayMapper medicalAssayMapper;

    @Autowired
    RedisUtil redisUtil;

    @RequestMapping(value = "cha")
    @ResponseBody
    public ModelAndView cha(@RequestParam(value ="hospitalId",defaultValue = "0") int hospitalId){

        List<MedicalAssay> medicalAssay;

        List<Hospital> medical2= medicalAssayMapper.HospByID();

        if(hospitalId == 0){
          medicalAssay = medicalAssayMapper.MedicaByID();
        }else{

            medicalAssay = medicalAssayMapper.MedicaBy(hospitalId);
        }

        //往redis中存入对象

        String result= JSONObject.toJSONString(medicalAssay);

        redisUtil.set("emp",result);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("medical",medicalAssay);

        modelAndView.addObject("medical2",medical2);

        modelAndView.addObject("hospitalId",hospitalId);

        modelAndView.setViewName("zhanshi");

        return modelAndView;
    }


    @RequestMapping(value = "/add")
    public ModelAndView addMedic(){
        ModelAndView modelAndView=new ModelAndView();
        List<Hospital> medical2= medicalAssayMapper.HospByID();
        modelAndView.addObject("medical2",medical2);


        modelAndView.setViewName("addMedica");
        return modelAndView;
    }

    @RequestMapping(value = "addMedica")
    public String addMedica(MedicalAssay medicalAssay){
        ModelAndView modelAndView=new ModelAndView();

        int yes=medicalAssayMapper.addMedica(medicalAssay);

        if(yes>0){
            System.out.println("新增成功");
        }else{
            System.out.println("新增失败");
        }

        return "redirect:/cha";

    }

    @RequestMapping(value = "que")
    public String que(int id){
        int yes=medicalAssayMapper.updataMedica(id);

        if(yes>0){
            System.out.println("新增成功");
        }else{
            System.out.println("新增失败");
        }

        return "redirect:/cha";
    }
}
